from django.db import models
from django.contrib import auth

class Instance(models.Model):
    """A raid instance"""
    name = models.CharField(max_length=200)
    class Meta:
        ordering = ['name',]
    def __unicode__(self):
        return self.name

class Boss(models.Model):
    """A raid Boss"""
    instance = models.ForeignKey(Instance)
    name = models.CharField(max_length=200)
    class Meta:
        ordering = ['name',]
        verbose_name_plural = "Bosses"
    def __unicode__(self):
        return self.name

class Item(models.Model):
    """An item of loot"""
    boss = models.ForeignKey(Boss)
    name = models.CharField(max_length=200)
    class Meta:
        ordering = ['name',]
    def __unicode__(self):
        return self.name
    def instance(self):
        return self.boss.instance

class Raid(models.Model):
    """A raid"""
    instance = models.ForeignKey(Instance)
    characters_required = models.IntegerField()
    start_time = models.DateTimeField('Starts')
    ends = models.CharField(max_length=200)
    class Meta:
        ordering = ['-start_time',]
    def __unicode__(self):
        return "%s : %s" % (self.instance.__unicode__(), self.start_time) 
    def signups(self):
        return Raider.objects.filter(raid=self.id).count()
    def raiders(self):
        return Raider.objects.filter(raid=self.id, confirmed=True)
    def isInRaid(self, character):
        return Raider.objects.filter(raid=self.id, confirmed=True, character=character).count()
    def __cmp__(self, other):
        return cmp(self.start_time, other.start_time)
    
class CharacterClass(models.Model):
    """The games character classes"""
    hex_colour = models.CharField(max_length=6)
    icon = models.CharField(max_length=24)
    name = models.CharField(max_length=20)
    class Meta:
        ordering = ['name']
        verbose_name_plural = "Character Classes"
    def __unicode__(self):
        return self.name
    

class Character(models.Model):
    """A player character"""
    player = models.ForeignKey(auth.models.User, null=True, blank=True)
    character_class = models.ForeignKey(CharacterClass)
    name = models.CharField(max_length=20)
    class Meta:
        ordering = ['name']
    def __unicode__(self):
        return self.name + " : " + self.character_class.__unicode__()

class Raider(models.Model):
    raid = models.ForeignKey(Raid)
    character = models.ForeignKey(Character)
    signup_date = models.DateTimeField('time of sign up', null=True, blank=True)
    cancel_date = models.DateTimeField('time of cancellation', null=True, blank=True)
    confirmed = models.BooleanField()
    note = models.CharField(max_length=200, blank=True)
    class Meta:
        ordering = ['character',]
    def __unicode__(self):
        return self.character.__unicode__()

class RaidLoot(models.Model):
    raid = models.ForeignKey(Raid)
    character = models.ForeignKey(Character)
    boss = models.ForeignKey(Boss)
    item = models.ForeignKey(Item)
    note = models.CharField(max_length=200, blank=True)
    class Meta:
        ordering = ['item',]
    def __unicode__(self):
        return "%s looted %s" %(self.raid.__unicode__(), self.item.__unicode__())
    def ordinal(self):
        return self.pk   

